DR='N:'
DOC_FEE=0
DOC_ALIAS=""
SELE 1
LOCA FOR INV_NBER=NOFT
DO WHILE .T.
   IF FOUND() .AND. .NOT. EOF()
      IF REC_LOCK()
         REPL INV_STAT WITH 'V'+KPIX
      ENDIF
      UNLOCK
      CONTINUE
      LOOP
   ELSE
      EXIT
   ENDIF
ENDDO

SELE 2
LOCA FOR INV_NBER=NOFT
DO WHILE .T.
   IF FOUND() .AND. .NOT. EOF()
      IF REC_LOCK()
         REPL INV_STAT WITH 'V'+KPIX
      ENDIF
      UNLOCK
      CONTINUE
      LOOP
   ELSE
      EXIT
   ENDIF
ENDDO

SELE 4
LOCA FOR INV_NBER=NOFT
DO WHILE .T.
   IF FOUND() .AND. .NOT. EOF()
      IF REC_LOCK()
         REPL INV_STAT WITH 'V'+KPIX
      ENDIF
      UNLOCK
      CONTINUE
      LOOP
   ELSE
      EXIT
   ENDIF
ENDDO
SELE 5
LOCA FOR INV_NBER=NOFT
DO WHILE .T.
   IF FOUND() .AND. .NOT. EOF()
      IF REC_LOCK()
         REPL INV_STAT WITH 'V'+KPIX
      ENDIF
      UNLOCK
      CONTINUE
      LOOP
   ELSE
      EXIT
   ENDIF
ENDDO
SELE 6
LOCA FOR INV_NBER=NOFT
DO WHILE .T.
   IF FOUND() .AND. .NOT. EOF()
      IF REC_LOCK()
         REPL INV_STAT WITH 'V'+KPIX
      ENDIF
      UNLOCK
      CONTINUE
      LOOP
   ELSE
      EXIT
   ENDIF
ENDDO
FILM='ME'+kdpat+'.dbf'
F8='MEDICAL'
SELE 8
SET EXCLU OFF
USE &DR&F8
COPY STRU TO &DR&FILM
USE
SET EXCLU OFF
USE &DR&FILM
APPEN FROM &DR&F1 FOR INV_NBER=NOFT
COUNT TO NPROC
NBER=VAL(VISIT_NBER)+1
NOBER=STR(NBER,5)
GO TOP
DO WHILE .NOT. EOF()
   ARKOD=AREA_CODE
   SUKOD=SUB_AREACO
   PRKOD=PRO_CODE
   F9='DOCT_FEE'
   SELE 9
   SET EXCLU OFF
   USE &DR&F9 ALIAS DOCT_FEA
   GO TOP
   LOCAT FOR AREA_CODE=ARKOD .AND. SUB_AREACO=SUKOD .AND. PRO_CODE=PRKOD .AND. INV_NBER=NOFT
   DOCFE=DOC_FEE
   DOCNM=DOC_ALIAS
   IF FOUND()
      SELE 8
      IF REC_LOCK()
         REPL COST_ALLO1 WITH DOCFE
         REPL DOC_ALIAS WITH DOCNM
      ENDIF
      UNLOCK
      SKIP
   ELSE
      SELE 8
      SKIP
   ENDIF
ENDDO
SELE 9
USE
FILP='PH'+kdpat+'.dbf'
F8='PHARMACY'
SELE 8
SET EXCLU OFF
USE &DR&F8
COPY STRU TO &DR&FILP
USE
SET EXCLU OFF
USE &DR&FILP
APPEN FROM &DR&F2 FOR INV_NBER=NOFT
USE &DR&FILP
GO TOP
DO WHILE .NOT. EOF()
   KDPHAR=PHAR_CODE
   XTEM=PHAR_X
   NMPHAR=SPACE(16)
*   IF SUBSTR(KDPHAR,2,1)='6' .OR. SUBS(KDPHAR,2,1)='7' .OR. SUBSTR(KDPHAR,2,1)='9'

   F9='DISPOS'
   SELE 9
   USE &DR&F9 INDEX &DR&F9
   SEEK KDPHAR
   IF .NOT. EOF()
      NMPHAR=DISP_NAME
      IF REC_LOCK(0)
         REPL ST_ACT_TOT WITH ST_ACT_TOT+XTEM
         REPL ST_ACT_BUY WITH INT(ST_ACT_TOT/BUY_SELL_R)
         REPL ST_ACT_SEL WITH ST_ACT_TOT-(ST_ACT_BUY*BUY_SELL_R)
      ENDIF
      UNLOCK
   ELSE
      F9='DRUGS'
      SELE 9
      USE &DR&F9 INDEX &DR&F9
      SEEK KDPHAR
      IF .NOT. EOF()
         NMPHAR=DRUG_NAME
         IF REC_LOCK(0)
            REPL ST_ACT_TOT WITH ST_ACT_TOT+XTEM
            REPL ST_ACT_BUY WITH INT(ST_ACT_TOT/BUY_SELL_R)
            REPL ST_ACT_SEL WITH ST_ACT_TOT-(ST_ACT_BUY*BUY_SELL_R)
         ENDIF
         UNLOCK
      ENDIF
   ENDIF
   SELE 8
   IF REC_LOCK()
      REPL PHAR_NAME WITH NMPHAR
   ENDIF
   UNLOCK
   SKIP
ENDDO
RETURN
